<?php
require_once (CAMINHO_SERVIDOR . "configs.php");
require_once (PASTA_ERROR . "ExceptionGeral.class.php");
require_once (PASTA_SMARTY . "ConfigSmarty.class.php");
require_once (PASTA_SMARTY_LIB . 'Smarty.class.php');
require_once(PASTA_SMARTY . "Cryptografa.class.php");
require_once(PASTA_CLASSES . "Browser.class.php");
require_once(PASTA_CLASSES . "Usuarios.class.php");
require_once (PASTA_CONEXAO . 'Conexao.class.php');
define("CABECALHO", PASTA_SMARTY . "templates/cabecalho.tpl");
define("CONTEUDO", PASTA_SMARTY . "templates/conteudo.tpl");
define("RODAPE", PASTA_SMARTY . "templates/rodape.tpl");
define("INDEX", PASTA_SMARTY . "templates/index.tpl");

class Inicia {

    private $objSmarty;
    private $objRedirect;
    private $objCripty = null;
    private $objCabecalho = null;
    private $objrodape = null;
    private $objConteudo = null;
    private $objIndex = null;
    private $objBrowser = null;
    private $objUser = null;

    public function __construct() {
        
    }

    public function getObjSmarty() {
        if ($this->objSmarty == null)
            $this->objSmarty = ConfigSmarty::getConfigSmarty();
        return $this->objSmarty;
    }

    public function getConexao() {
        if ($this->objCon == null)
            $this->objCon = Conexao::getConexao();
        return $this->objCon;
    }

    public function getObjCripty() {
        if ($this->objCripty == null)
            $this->objCripty = new Cryptografa();
        return $this->objCripty;
    }

    public function getObjCabecalho() {
        if ($this->objCabecalho == null)
            $this->objCabecalho = CAMINHO_SERVIDOR . "cabecalho.tpl";
        return $this->objCabecalho;
    }

    public function getObjRodape() {
        if ($this->objRodape == null)
            $this->objRodape = CAMINHO_SERVIDOR . "rodape.tpl";
        return $this->objRodape;
    }

    public function getObjConteudo() {
        if ($this->objConteudo == null)
            $this->objConteudo = CAMINHO_SERVIDOR . "conteudo.tpl";
        return $this->objConteudo;
    }

    public function getObjIndex() {
        if ($this->objIndex == null)
            $this->objIndex = CAMINHO_SERVIDOR . "index.tpl";
        return $this->objIndex;
    }

    public function getObjBrowser() {
        if ($this->objBrowser == null)
            $this->objBrowser = New Browser();
        return $this->objBrowser;
    }

    public function getObjUsuario() {
        if ($this->objUser == null)
            $this->objUser = New Usuarios();
        return $this->objUser;
    }

    public function getDadosPortal() {
        $sql = "SELECT * FROM tb_informacoes_portal WHERE id_informacoes=1";
        $dados = self::getConexao()->GetRow($sql);
        $sql = "SELECT descricao FROM tb_area WHERE area='" . Funcoes::getPastaDoCaminho() . "'";
        $titulo = end(self::getConexao()->GetRow($sql));
        if (empty($titulo)) {
            $sql = "SELECT descricao FROM tb_sub_area WHERE area='" . Funcoes::getPastaDoCaminho() . "'";
            $titulo = end(self::getConexao()->GetRow($sql));
        }
        if (!empty($titulo)) {
            $titulo .= " - ";
        }
        $sql = "SELECT * FROM tb_area WHERE area='" . Funcoes::getPastaDoCaminho() . "'";
        $outros_dados_area = self::getConexao()->GetRow($sql);
        $sql = "SELECT * FROM tb_sub_area WHERE area='" . Funcoes::getPastaDoCaminho() . "'";
        $outros_dados_sub_area = self::getConexao()->GetRow($sql);
        //Funcoes::debuga( $outros_dados_area);
        self::getConexao()->Close();
        if (!empty($outros_dados_sub_area["descricao_pagina"])) {
            self::getObjSmarty()->assign("DESCRICAO_PORTAL", $outros_dados_sub_area["descricao_pagina"]);
        } else if (!empty($outros_dados_area["descricao_pagina"])) {
            self::getObjSmarty()->assign("DESCRICAO_PORTAL", $outros_dados_area["descricao_pagina"]);
        }else{
            self::getObjSmarty()->assign("DESCRICAO_PORTAL", $dados["descricao_portal"]);     
        }
        self::getObjSmarty()->assign("RESUMO", $dados["resumo_portal"]);
        self::getObjSmarty()->assign("PALAVRAS_CHAVE", $dados["palavras_chave_portal"]);
        self::getObjSmarty()->assign("TITULO", ($dados["titulo_portal"] == null) ? $titulo . NOME_PUBLICO : $titulo . $dados["titulo_portal"]);
        self::getObjSmarty()->assign("ICO", $dados["nome_foto"]);
    }

    public function getDadosUsuario() {
        $this->getObjSmarty()->assign("ID_USUARIO", self::getObjUsuario()->getIdUsuario());
        $this->getObjSmarty()->assign("NOME_USUARIO", self::getObjUsuario()->getNomeUsuario());
    }

    public function setTitulo($strPag) {
        $this->getObjSmarty()->assign("TITULO", $strPag);
    }

    public function novaTag($nomeTag, $valorTag) {
        $this->getObjSmarty()->assign($nomeTag, $valorTag);
    }

    public function getBibliotecas($modulo) {
        if ($modulo == "admin") {
            self::getObjSmarty()->assign("FORMATA_TEXT_AREAS", self::getObjSmarty()->fetch(PASTA_CONTEUDO_MODEL . "tinymce.tpl"));
        } else {
            self::getObjSmarty()->assign("COMPARTILHAMENTO", self::getObjSmarty()->fetch(PASTA_CONTEUDO_MODEL . "addthis.tpl"));
            self::getObjSmarty()->assign("DADOS_ANALYTICS", self::getObjSmarty()->fetch(PASTA_CONTEUDO_MODEL . "analytics.tpl"));
        }
    }

    public function imprimeTela($cabecalho = CABECALHO, $conteudo = CONTEUDO, $rodape = RODAPE, $default = INDEX, $exception = null, $modulo = null, $html_conteudo = null) {
        if ($html_conteudo) {
            self::getObjSmarty()->assign("HTML_CONTEUDO", $html_conteudo);
        }
        self::getObjSmarty()->assign("PASTA_IMAGENS", URL_PASTA_IMAGENS);
        self::getObjSmarty()->assign("MENSAGEM", self::getObjCripty()->decrypt($_GET["msg"]));
        define("MSG", self::getObjCripty()->decrypt($_GET["msg"]));
        define("TITULO_MSG", self::getObjCripty()->decrypt($_GET["msgTitulo"]));
        define("TIPO_MSG", self::getObjCripty()->decrypt($_GET["tipoMsg"]));
        self::getObjSmarty()->assign("MSG_EXCEPTION", $exception);
        self::getDadosUsuario();
        self::getObjSmarty()->assign("CABECALHO", self::getObjSmarty()->fetch($cabecalho));
        self::getObjSmarty()->assign("CONTEUDO", self::getObjSmarty()->fetch($conteudo));
        self::getObjSmarty()->assign("RODAPE", self::getObjSmarty()->fetch($rodape));
        self::getObjSmarty()->assign("MENU_PERFIL", self::getObjSmarty()->fetch(PASTA_CONTEUDO_MODEL . "menuPerfilAdminUsuario.tpl"));
        self::getObjSmarty()->assign("URL_SERVIDOR", URL);
        self::getObjSmarty()->assign("CANONICAL", Funcoes::getUrlAtualLimpa());
        $navegador = self::getObjBrowser()->Name;
        self::getDadosPortal();
        self::getBibliotecas($modulo);
        $versao = self::getObjBrowser()->Version;
        $plataforma = self::getObjBrowser()->Platform;
        if (strstr($navegador, "Firefox")) {
            $navegador = $navegador . substr(str_replace('.', '', $versao), 0, 2);
        } else {
            $navegador = $navegador . substr(str_replace('.', '', $versao), 0, 1);
        }
        self::getObjSmarty()->assign("NAVEGADOR", $navegador);
        self::getObjSmarty()->assign("PLATAFORMA", $plataforma);
        self::getObjSmarty()->display($default);
    }

}

?>